package com.winit.feature.repository;

import com.winit.feature.model.MFeature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by wangjian on 2017/9/28.
 */
@Component
public class MFeatureOperations {

    private static Logger logger = LoggerFactory.getLogger(MFeatureOperations.class);

    @PersistenceContext
    private EntityManager entityManager;

    @Autowired
    MFeatureRepository repository;

    public void updateFeature(MFeature feature) {
        CriteriaBuilder cb = entityManager.getCriteriaBuilder();

        CriteriaUpdate<MFeature> op = cb.createCriteriaUpdate(MFeature.class);
        Root<MFeature> root = op.from(MFeature.class);
        if (null != feature.getStatus()) {
            op.set("status", feature.getStatus());
        }
        if (null != feature.getReqId()) {
            op.set("req_id", feature.getReqId());
        }
        if (null != feature.getAttrConfigId()) {
            op.set("attr_config_id", feature.getAttrConfigId());
        }
        if (null != feature.getFeatureName()) {
            op.set("feature_name", feature.getFeatureName());
        }
        if (null != feature.getSys()) {
            op.set("sys", feature.getSys());
        }
        if (null != feature.getOriginalPageId()) {
            op.set("original_page_id", feature.getOriginalPageId());
        }
        if (null != feature.getType()) {
            op.set("type", feature.getType());
        }
        op.where(cb.equal(root.get("id"), feature.getId()));
        entityManager.createQuery(op).executeUpdate();
    }

    public List<MFeature> findByProperNotNull(MFeature feature) {
        CriteriaBuilder cb = entityManager.getCriteriaBuilder();

        CriteriaQuery<MFeature> query = cb.createQuery(MFeature.class);
        Root<MFeature> root = query.from(MFeature.class);
        List<Predicate> predicates = new ArrayList();
        if (null != feature.getId()) {
            predicates.add(cb.equal(root.get("id"), feature.getId()));
        }
        if (null != feature.getStatus()) {
            predicates.add(cb.equal(root.get("status"), feature.getStatus()));
        }
        if (null != feature.getReqId()) {
            predicates.add(cb.equal(root.get("req_id"), feature.getReqId()));
        }
        if (null != feature.getAttrConfigId()) {
            predicates.add(cb.equal(root.get("attr_config_id"), feature.getAttrConfigId()));
        }
        if (null != feature.getFeatureName()) {
            predicates.add(cb.equal(root.get("feature_name"), feature.getFeatureName()));
        }
        if (null != feature.getSys()) {
            predicates.add(cb.equal(root.get("sys"), feature.getSys()));
        }
        if (null != feature.getOriginalPageId()) {
            predicates.add(cb.equal(root.get("original_page_id"), feature.getOriginalPageId()));
        }
        if (null != feature.getType()) {
            predicates.add(cb.equal(root.get("type"), feature.getType()));
        }
        query.where(predicates.toArray(new Predicate[0]));
        return entityManager.createQuery(query).getResultList();
    }
}
